/**
 * Framework for batch testing user profile models
 * Final project by Sergey Nepomnyachiy and Julia Polchin
 * Supervisor: Tsvi Kuflik
 *
 */

package core;

import interfaces.ComponentInterface;
import interfaces.LoggingInterface;
import interfaces.ProgressKeeperInterface;

import java.util.Vector;


/**
 * Class representing the step element of XML
 * @author Sergey and Julia
 * 
 */
public class Step implements ComponentInterface {
	/**
	 * 
	 * Logging backend reference
	 */
	@SuppressWarnings("unused")
	private LoggingInterface fs;
	/**
	 * The object of invocation. Null for static methods
	 */
	@SuppressWarnings("unused")
	private Object instance;
	/**
	 * Nested methods
	 */
	private Vector<MethodCapsule> methods;

	/**
	 * Setter for the instance to run on
	 * May be null for static methods
	 * @param instance
	 *            the instance to set
	 */
	public void setInstance(Object instance) {
		this.instance = instance;
		for (MethodCapsule mc : this.methods) 
			mc.setInstance(instance);
	}

	/**
	 * Constructor of a step
	 * @param fs Logging backend 
	 */
	public Step(LoggingInterface fs) {
		this.fs = fs;
		this.methods = new Vector<MethodCapsule>();
		this.instance = null;

	}

	@Override
	public boolean execute() {
		boolean flag = true;
		while (flag) {
			for (MethodCapsule mc : this.methods) 
				flag = flag && mc.execute();
				
		}
		
		return true;
	}

	/**
	 * Per user execution of method 
	 * @param index Use generated_data[index] as arguments storage 
	 * @return false if done with the data, true if can run some more.
	 */
	public boolean execute(int index) {
		boolean flag = true;
		while (flag) {
			for (MethodCapsule mc : this.methods)
				flag = flag && mc.execute(index);
		}
		return true;
	}


	@Override
	public void setFs(LoggingInterface fs) {
		this.fs = fs;
		for (MethodCapsule mc : this.methods)
			mc.setFs(fs);
	}

	/**
	 * Adding nested methods
	 * @param method new method
	 */
	public void addMethod(MethodCapsule method) {
		this.methods.add(method);
	}

	@Override
	public void setProgresser(ProgressKeeperInterface pk) {
		//Auto-generated method stub
		
	}

}